<template>
  <div class="account-settings-info-view">
    <a-row :gutter="16" type="flex" justify="center">
      <a-col :order="1" :md="24" :lg="16">
        <a-form layout="vertical">
          <a-form-item label="姓名">
            <ZgInput :width="200" placeholder="请输入您的姓名!" v-model="user.name"></ZgInput>
          </a-form-item>
          <a-form-item label="性别">
            <ZgRadio v-model="user.sex" :options="[{value:'1',label:'男'},{value:'2',label:'女'}]"></ZgRadio>
          </a-form-item>
          <a-form-item label="生日">
            <ZgDatePicker v-model="user.birth"></ZgDatePicker>
          </a-form-item>
          <a-form-item label="个人简介">
            <ZgTextarea placeholder="请输入个人简介!" v-model="user.intro"></ZgTextarea>
          </a-form-item>
          <a-form-item>
            <a-button type="primary" @click="saveUserInfo">保存基本信息</a-button>
          </a-form-item>
        </a-form>
      </a-col>
      <a-col :order="1" :md="24" :lg="8" :style="{ minHeight: '180px' }">
        <div class="ant-upload-preview" @click="$refs.modal.edit(1)" >
          <a-icon type="cloud-upload-o" class="upload-icon"/>
          <div class="mask">
            <a-icon type="plus" />
          </div>
          <img :src="MINIO_URL + user.head"/>
        </div>
      </a-col>
    </a-row>
    <avatar-modal ref="modal" @ok="setAvatar"/>
  </div>
</template>

<script>
import AvatarModal from './AvatarModal'
import { mapMutations } from 'vuex'
export default {
  components: {
    AvatarModal
  },
  data () {
    return {
      user: {
        id: '0',
        head: '/upload/head.png',
        name: '',
        sex: '1',
        birth: '',
        intro: ''
      }
    }
  },
  mounted () {
    this.searchUserInfo()
  },
  methods: {
    ...mapMutations(['SET_NAME', 'SET_AVATAR']),
    searchUserInfo () {
      this.$http.get('/zg-system/user/info', {}, data => {
        this.user.id = data.id
        this.user.head = data.head
        this.user.name = data.name
        this.user.sex = data.sex
        this.user.birth = data.birth
        this.user.intro = data.intro
      })
    },
    setAvatar (url) {
      this.user.head = url
    },
    saveUserInfo () {
      if (this.user.name.length === 0) {
        this.$warning({ title: '请输入姓名' })
        return
      }
      this.$http.post('/zg-system/user/save', this.user, () => {
        this.SET_NAME(this.user.name)
        this.SET_AVATAR(this.MINIO_URL + this.user.head)
        this.$success({ title: '操作成功' })
      })
    }
  }
}
</script>

<style lang="less" scoped>

  .ant-upload-preview {
    position: relative;
    margin: 0 auto;
    width: 100%;
    max-width: 180px;
    border-radius: 50%;
    box-shadow: 0 0 4px #ccc;

    .upload-icon {
      position: absolute;
      top: 0;
      right: 10px;
      font-size: 1.4rem;
      padding: 0.5rem;
      background: rgba(222, 221, 221, 0.7);
      border-radius: 50%;
      border: 1px solid rgba(0, 0, 0, 0.2);
    }
    .mask {
      opacity: 0;
      position: absolute;
      background: rgba(0,0,0,0.4);
      cursor: pointer;
      transition: opacity 0.4s;

      &:hover {
        opacity: 1;
      }

      i {
        font-size: 2rem;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-left: -1rem;
        margin-top: -1rem;
        color: #d6d6d6;
      }
    }

    img, .mask {
      width: 100%;
      max-width: 180px;
      height: 100%;
      border-radius: 50%;
      overflow: hidden;
    }
  }
</style>
